<script lang="ts" setup>
import { ref, reactive, onMounted, nextTick } from 'vue';
import { CustomMouseMenu } from '@/plugins/directive/contextmenu';
import { ElMessageBox, ElMessage } from 'element-plus';
import useStore from '@/store';
import { props } from './dictionary';
import { LicenseEnum } from '@/enums/licence';
import { WindowType } from '@/types/global/window';
defineOptions({ name: 'Dictionary' });
defineProps(props);
//如const props = defineProps({ name: { type:String, defaule:'' }})
const tableCategoryRef = ref(null);
const loading = ref(true);
const dictionaryCategorys = ref(null);
const currentCategoryRow = ref(null);
const currentRow = ref(null);
const totalCount = ref(0);
const params = reactive({
  pageIndex: 1,
  pageSize: 20,
  query: {
    category: null,
    name: null,
  },
});
const handleTableRowClick = row => {
  currentCategoryRow.value = row;
  nextTick(() => {
    tableCategoryRef.value.setCurrentRow(currentCategoryRow.value);
  });
};
const remove = () => {
  if (!currentCategoryRow.value) {
    ElMessage.error('请选择要删除的字典类型!');
    return;
  }
  let confirmMessage = '是否删除选中的模块?';
  // if (ids.constructor === Number) {
  //   confirmMessage = '是否删除模块（' + name + '）?';
  //   ids = [ids];
  // }

  ElMessageBox.confirm(confirmMessage, '提示', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning',
  })
    .then(() => {
      // window.$api.module.remove(ids).then(res => {
      //   ElMessage.success('删除成功');
      //   query();
      // });
    })
    .catch(() => {});
};
const query = async () => {
  loading.value = true;
  await window.$api.dictionary.query(params).then(res => {
    dictionaryCategorys.value = res.data.list;
    params.pageIndex = res.data.pageIndex;
    params.pageSize = res.data.pageSize;
    totalCount.value = res.data.totalCount;
    loading.value = false;
  });
};
const add = () => {
  if (!currentCategoryRow.value) {
    ElMessage.error('请选择要添加字典的字典类型!');
    return;
  }
  const window: WindowType = {
    name: 'DictionaryForm',
    component: 'DictionaryForm',
    label: '(' + currentCategoryRow.value.name + ')字典添加',
    icon: 'edit',
    width: 400,
    height: 380,
    params: { dictionaryCategoryId: currentCategoryRow.value.id },
  };
  useStore.window.windowAdd(window);
};
const edit = () => {
  const window: WindowType = {
    name: 'DictionaryForm' + currentRow.value.id,
    component: 'DictionaryForm',
    label: '(' + currentCategoryRow.value.name + ')字典编辑（' + currentRow.value.name + '）',
    icon: 'edit',
    width: 400,
    height: 380,
    params: { id: currentRow.value.id },
  };
  useStore.window.windowAdd(window);
};
const dictionaryCategoryAdd = () => {
  const window: WindowType = { name: 'DictionaryCategoryForm', component: 'DictionaryCategoryForm', label: '字典类型添加', icon: 'edit', width: 400, height: 350 };
  useStore.window.windowAdd(window);
};
const dictionaryCategoryEdit = () => {
  if (!currentCategoryRow.value) {
    ElMessage.error('请选择要修改的字典类型!');
    return;
  }
  const window: WindowType = {
    name: 'DictionaryCategoryForm' + currentCategoryRow.value.id,
    component: 'DictionaryCategoryForm',
    label: '字典类型编辑（' + currentCategoryRow.value.name + '）',
    icon: 'edit',
    width: 400,
    height: 350,
    params: { id: currentCategoryRow.value.id },
  };
  useStore.window.windowAdd(window);
};

const categoryMenus = reactive([
  {
    label: '修改',
    icon: 'Edit',
    fn: () => {
      dictionaryCategoryEdit();
    },
    license: LicenseEnum.SYS_DICTIONARYCATEGORY_MODIFY,
  },
  {
    label: '删除',
    icon: 'Delete',
    fn: () => {
      remove();
    },
    license: LicenseEnum.SYS_DICTIONARYCATEGORY_REMOVE,
  },
  {
    label: '字典添加',
    icon: 'Edit',
    fn: () => {
      add();
    },
    license: LicenseEnum.SYS_DICTIONARY_ADD,
  },
]);
const showCategoryMouseMenu = (row, column, event) => {
  const ctx = CustomMouseMenu({
    el: event.currentTarget,
    mouse: event,
    menus: categoryMenus,
  });
  ctx.show();
  currentCategoryRow.value = row;
  tableCategoryRef.value.setCurrentRow(row);
};
const menus = reactive([
  {
    label: '修改',
    icon: 'Edit',
    fn: () => {
      edit();
    },
    license: LicenseEnum.SYS_DICTIONARY_MODIFY,
  },
  {
    label: '删除',
    icon: 'Delete',
    fn: () => {
      remove();
    },
    license: LicenseEnum.SYS_DICTIONARY_REMOVE,
  },
]);
const showMouseMenu = (row, column, event, categoryRow) => {
  const ctx = CustomMouseMenu({
    el: event.currentTarget,
    mouse: event,
    ...{ menus },
  });
  ctx.show();
  currentRow.value = row;
  currentCategoryRow.value = categoryRow;
  tableCategoryRef.value.setCurrentRow(categoryRow);
};
onMounted(() => {
  query();
});
</script>

<template>
  <windows-window :window="window">
    <template #button>
      <windows-button-group size="18">
        <windows-button icon="CirclePlus" :license="LicenseEnum.SYS_DICTIONARYCATEGORY_ADD" @click="dictionaryCategoryAdd">新建</windows-button>
        <windows-button icon="Edit" :license="LicenseEnum.SYS_DICTIONARYCATEGORY_MODIFY" split @click="dictionaryCategoryEdit"></windows-button>
        <windows-button icon="Delete" :license="LicenseEnum.SYS_DICTIONARYCATEGORY_REMOVE"></windows-button>
      </windows-button-group>
    </template>
    <template #search>
      <el-form ref="form" :model="params.query" label-width="auto" label-position="right" size="small">
        <el-row>
          <el-col :span="8">
            <el-form-item label="字典类型名称">
              <el-input v-model="params.query.name" />
            </el-form-item>
          </el-col>
          <el-col :span="2" style="text-align: right">
            <el-button type="primary" icon="Search" @click="query">搜索</el-button>
          </el-col>
        </el-row>
      </el-form>
    </template>
    <el-table
      ref="tableCategoryRef"
      :data="dictionaryCategorys"
      style="width: 100%"
      height="100%"
      highlight-current-row
      v-loading="loading"
      @row-contextmenu="showCategoryMouseMenu"
      @row-click="handleTableRowClick"
    >
      <el-table-column type="expand">
        <template #default="props">
          <div style="padding: 10px 20px">
            <el-table
              :data="props.row.dictionaries"
              style="width: 100%"
              height="100%"
              border
              @row-contextmenu="
                (row, column, event) => {
                  showMouseMenu(row, column, event, props.row);
                }
              "
            >
              <el-table-column type="index" width="80" label="序号" align="center" />
              <el-table-column label="字典名称" prop="name" width="150" />
              <el-table-column label="字典值" prop="value" width="150" />
              <el-table-column label="排序" prop="sort" width="150" />
              <el-table-column label="备注" prop="remark" />
            </el-table>
          </div>
        </template>
      </el-table-column>
      <el-table-column type="index" width="80" label="序号" align="center" />
      <el-table-column prop="category" label="字典类型" width="150" align="center"></el-table-column>
      <el-table-column prop="name" label="字典类型名称" width="150" align="center"></el-table-column>
      <el-table-column prop="remark" label="备注" align="center"></el-table-column>
    </el-table>
    <template #pagination>
      <el-pagination
        v-model:currentPage="params.pageIndex"
        v-model:page-size="params.pageSize"
        :page-sizes="[20, 30, 40, 50]"
        small="small"
        layout="total, sizes, prev, pager, next, jumper"
        :total="totalCount"
      />
    </template>
  </windows-window>
</template>

<style scoped lang="scss">
.el-tree {
  width: 180px;
}
.el-card {
  margin: 10px 20px;
  .card-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    .card-header-btn {
      color: #606266;
      padding: 8px;
      border-radius: 5px;
      cursor: pointer;
      display: flex;
      .el-icon {
        font-size: 20px;
      }
      &:hover {
        background: #e5e5e5;
      }
    }
  }

  .el-tag {
    margin: 5px;
  }
}
</style>
